<?php

/**
 * 特价商品
 *
 * @author libaoling
 * @time 2014-12-3
 */
class SaleApp extends MallbaseApp
{

    private $_goods_mod;
    private $_gcategory_mod;
    private $_recommend_mod;
    protected $page_size = 12;

    public function __construct()
    {
        $this->FeatureApp();
    }

    public function FeatureApp()
    {
        parent::__construct();

        $this->_goods_mod = m("goods");
        $this->_recommend_mod =& m('recommend');
        $this->_gcategory_mod =& bm('gcategory', array('_store_id' => 0));
    }

    public function index()
    {
        $condition = "";
        $cate_id = empty($_GET['cate_id']) ? 0 : intval($_GET['cate_id']);
        
        //广告
        $adv_mod = & m('banner');
        $this->assign('adv_list', $adv_mod->_get_ad(8)); //所有的广告图片  
        //获取有商品的二级分类
        $cates = $this->_gcategory_mod->get_by_cate_ids($this->_get_cate_ids_by_goods());
        //最新特价
        $new_goods_ids = array();
        $new_sales = $this->_recommend_mod->get_recommended_goods(18, 4, true);
        foreach ($new_sales as $new_sale)
            $new_goods_ids[] = $new_sale['goods_id'];
        
        if($new_goods_ids)
            $condition .= " AND goods_id NOT " . db_create_in($new_goods_ids);
        
        $cate_id && $cate_ids = $this->_gcategory_mod->get_descendant_ids($cate_id, true);
        if(isset($cate_ids) && $cate_ids){
            $condition .= " AND cate_id_2 = {$cate_ids[0]}";
            unset($cate_ids[0]);
            $cate_ids && $condition .= " AND cate_id_3 " . db_create_in($cate_ids);
        }

        /* 分页信息 */
        $page = $this->_get_page($this->page_size);
        
        $cacheServer = cache_server();
        $cacheKey = "sale_index_" . md5($condition . $page['limit']);
        $data = $cacheServer->hGet($this->_goods_mod->_name, $cacheKey);
        if($data === false){
            $sale_list = $this->_goods_mod->find(array(
                'conditions' => 'g.if_show = 1 AND is_cheap = 1' . $condition,
                'fields' => 'goods_name,market_price,price,default_image',
                'order' => 'goods_id DESC',
                'count' => true,
                'limit' => $page['limit'],
            ));
            
            $page['item_count'] = $this->_goods_mod->getCount();
            $this->_format_page($page);
            
            $data['list'] = $sale_list;
            $data['page'] = $page;
            $cacheServer->hSet($this->_goods_mod->_name, $cacheKey, $data, $this->_goods_mod->expire);
        }
        
        foreach ($data['list'] as $key => $goods)
        {
            empty($goods['default_image']) && $data['list'][$key]['default_image'] = Conf::get('default_goods_image');
        }
        
        $this->assign('page_info', $data['page']); //分页数据
        $this->assign('sale_list', $data['list']); //商品列表
        $this->assign('cates', $cates); //二级分类
        $this->assign('new_sales', $new_sales); //最新特色
        /* 配置seo信息 */
        $this->_config_seo($this->_get_seo_info());

        $this->display("special_price.html");
    }
    
    private function _get_seo_info()
    {
        $result = array();

        return $result;
    }

    private function _get_cate_ids_by_goods()
    {
        $result = array();
        $goodses = $this->_goods_mod->find(array(
            'conditions' => 'if_show = 1 AND is_cheap = 1',
            'fields' => 'distinct cate_id_2',
        ));

        foreach ($goodses as $goods)
        {
            $result[] = $goods['cate_id_2'];
        }

        return $result;
    }
    
}
